import useSWR from "swr";
import { BASEURL } from "./config";

const fetcher = async (url) => {
  return await fetch(url, {
    method: "GET",
    headers: {
      "Content-Type": "application/json",
      Authorization: `Bearer ${localStorage.getItem("token")}`,
    },
  }).then((res) => res.json());
};

const swr = (url) => {
  const { data, error } = useSWR(BASEURL + url, fetcher, {
    refreshInterval: 2000,
  });

  return {
    data: data,
    isLoading: !error && !data,
    isError: error,
  };
};
export default swr;
